User-defined procedures are declared after the definition (CONST,
TYPE, and
VAR) blocks of a script, but before the script body. To create a user-defined procedure to use within a script, you will need to create a procedure declaration statement which associates an identifier with the subroutine and defines how the subroutine is to be used. The general syntax for user-defined procedures is:
The procedure declaration begins with the PROCEDURE keyword, and is followed by the identifier to be associated with the subroutine block. After this identifier comes the parameter list for the procedure. The parameter list provides a means for moving data in and out of the subroutine, and the identifiers in the list may be used just like variables within the subroutine block. Parameters and parameter lists will be covered in more detail later in this section.
After the procedure declaration statement has been created, the actual working code of the subroutine is defined. Just like a script, subroutines may have any of the standard VectorScript definition blocks (
LABEL,
CONST,
TYPE, or
VAR) as well as a script body containing the script code to be executed when the subroutine is called from elsewhere in your script. For example, suppose you wish to take the following script:
and modify it so that the sum of squares code can be easily reused whenever it is needed. To do this, a subroutine is needed to contain the code which performs the operation. Creating the subroutine begins by writing a procedure declaration statement and the skeleton of the subroutine:
The declaration statement associates the identifier SumOfSquares with the new subroutine. Following the subroutine identifier is the parameter list for the subroutine. This optional list defines a method of moving data in and out of the subroutine. While it is possible to refer to values in the enclosing program blocks directly, doing so would eliminate the ability to easily use the subroutine in other code, which is one of the major advantages of using subroutines.
The parameter list declares a set of identifiers (and their associated data types) that will be used to pass data to and from the subroutine; the
VAR keyword indicates an identifier that will be used to pass data out of the subroutine to the calling code. Identifiers in the parameter list can be treated as variables and used within the subroutine script code.
When the subroutine is called in the script, the parameter list as shown in the declaration is replaced with a list of variable identifiers that provide and/or receive the data being passed through the parameters. The order and types of the variable identifiers must exactly match those in the declaration.
Note again that when the script is called in the main program block, the SumOfSquares parameter list is replaced by the variables
n and
sum. The value contained in
n is passed into the subroutine, where it is referred to through the identifier
limit. The resulting value is stored in the local identifier
result, and is passed back to the main program block and stored in the variable
sum when the subroutine completes its execution.
By using a subroutine, the script can be broken up into manageable chunks which are easy to understand and to debug. The
SumOfSquares subroutine can also be reused as many times as needed in the current script, and the subroutine can be copied and used in other scripts.